package com.mmallbe.util;

import com.mmallbe.common.RedisShardedPool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.ShardedJedis;

@Slf4j
public class RedisShardedPoolUtil {

    /**
     * 重新设置key的有效期
     * @param key
     * @param exTime
     * @return Long 1 设置成功， 0 设置失败
     */
    public static Long expire(String key, int exTime){
        ShardedJedis jedis = null;
        Long result = null;
        try{
            jedis = RedisShardedPool.getJedis();
            result = jedis.expire(key ,exTime);
        }catch(Exception e){
            log.error("exire key:{} value:{} error",key,e);
            RedisShardedPool.returnBrokenResource(jedis);
            return result;
        }
        RedisShardedPool.returnBrokenResource(jedis);
        return result;
    }

    /**
     *  设置一个key 指定超时时间
     * @param key
     * @param value
     * @param exTime 单位是秒
     * @return
     */
    public static String setEx(String key, String value,int exTime){
        ShardedJedis jedis = null;
        String result = null;
        try{
            jedis = RedisShardedPool.getJedis();
            result = jedis.setex(key,exTime,value);
        }catch(Exception e){
            log.error("setEx key:{} value:{} error",key,value,e);
            RedisShardedPool.returnBrokenResource(jedis);
            return result;
        }
        RedisShardedPool.returnBrokenResource(jedis);
        return result;
    }
    public static String set(String key, String value){
        ShardedJedis jedis = null;
        String result = null;
        try{
            jedis = RedisShardedPool.getJedis();
            result = jedis.set(key,value);
        }catch(Exception e){
            log.error("set key:{} value:{} error",key,value,e);
            RedisShardedPool.returnBrokenResource(jedis);
            return result;
        }
        RedisShardedPool.returnBrokenResource(jedis);
        return result;
    }

    public static String get(String key){
        ShardedJedis jedis = null;
        String result = null;
        try{
            jedis = RedisShardedPool.getJedis();
            result = jedis.get(key);
        }catch(Exception e){
            log.error("get key:{} error",key,e);
            RedisShardedPool.returnBrokenResource(jedis);
            return result;
        }
        RedisShardedPool.returnBrokenResource(jedis);
        return result;
    }

    /**
     * 删除
     * @param key
     * @return 1  0
     */
    public static Long del(String key){
        ShardedJedis jedis = null;
        Long result = null;
        try{
            jedis = RedisShardedPool.getJedis();
            result = jedis.del(key);
        }catch(Exception e){
            log.error("del key:{} error",key,e);
            RedisShardedPool.returnBrokenResource(jedis);
            return result;
        }
        RedisShardedPool.returnBrokenResource(jedis);
        return result;
    }

//    public static void main(String[] args) {
//
//        ShardedJedis jedis = RedisShardedPool.getJedis();
//
//        set("geetykey","geetyValue");
//
//        get("geetykey");
//
//        setEx("geetySex","geetySex",100);
//        expire("geetySex",100);
//        del("geetykey");
//    }
}
